<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title><%= title %></title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <style>
    html, body {
      margin: 0;
      width: 100%; height: 100%;
    }
    .box {
      position: absolute;
      width: 800px;
      top: 50%; left: 50%;
      transform: translate(-50%, -50%);
    }
    .logined {
      text-align: center;
    }
    .logined span {
      color: brown;
    }
  </style>
</head>
<body>
  <p class="logined">此处是cookie，登录的证明：<span><%= cookie %></span></p>
  <div id="app">
    <div class="box">
      <el-row>
        <el-col :span="12" :offset="6">
          <el-form :model="data" ref="form" label-width="100px" class="demo-ruleForm">
            <el-form-item
              label="账号"
              prop="name"
              :rules="[
              { required: true, message: '账号不能为空'},
            ]">
              <el-input type="name" size="mini" v-model="data.name" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item
              label="密码"
              prop="pass"
              :rules="[
                { required: true, message: '密码不能为空'},
                { type: 'number', message: '密码必须为数字值'}
              ]">
              <el-input type="pass" size="mini" v-model.number="data.pass" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item :style="{textAlign: 'center'}">
              <el-button size="mini" class="btn" @click="ajax" type="primary">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        data: {
          name: '',
          pass: '',
        },
      },
      methods: {
        ajax() {
          this.$refs.form.validate((valid, a) => {
            if (valid) {
              const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
              });

              axios.get('/ajax', {
                params: {
                  name: this.data.name,
                  pass: this.data.pass,
                }
              }).then((res) => {
                console.log(res)
                if (res.data.code === 0) {
                  loading.close();
                  this.$message({
                    message: '登陆成功',
                    type: 'success'
                  });
                }
              });
            } else {
              return false;
            }
          });
        },
      }
    });
  </script>
</body>
</html>